import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { logger, LoggerConst } from '@ohos/logger';
import type Want from '@ohos.app.ability.Want';
import type AbilityConstant from '@ohos.app.ability.AbilityConstant';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onCreate.');
  }

  onDestroy(): void {
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        logger.error(LoggerConst.ENTRY_ABILITY_TAG, `Failed to load the content. Fause: ${JSON.stringify(err)}`);
        return;
      }

      // window的全屏能力
      windowStage.getMainWindow((err, windowClass) => {
        if (err.code) {
          logger.error(LoggerConst.ENTRY_ABILITY_TAG, `Failed to get main window. Fause: ${JSON.stringify(err)}`);
          return;
        }

        logger.info(LoggerConst.ENTRY_ABILITY_TAG, `Succeeded in get main window. Data: ${JSON.stringify(windowClass)}`);

        windowClass.setWindowLayoutFullScreen(true);
        windowClass.setWindowSystemBarEnable(['navigation']);

        // 设置系统默认规避区域
        windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
      })

      logger.info(LoggerConst.ENTRY_ABILITY_TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data)}`);
    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    logger.info(LoggerConst.ENTRY_ABILITY_TAG, 'Ability onBackground');
  }
}
